package com.difsonapps.android.marker;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.difsonapps.android.marker.app.AppController;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EditAccount0Activity extends Activity {
	View loadingLayout;
	View formScrollView;
	TextView loadingStatusMessage;
	TextView messageTextView;
	
	Button saveButton;
	
	EditText firstnameEditText;
	EditText lastnameEditText;
	EditText occupationEditText;
	EditText aboutmeEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_account0);
		

		
		loadingLayout = findViewById(R.id.loading_status);
		formScrollView = findViewById(R.id.formScrollView);
		
		loadingStatusMessage = (TextView) findViewById(R.id.loading_status_message);
		messageTextView = (TextView) findViewById(R.id.messageTextView);
		
		firstnameEditText = (EditText) findViewById(R.id.firstnameEditText);
		lastnameEditText = (EditText) findViewById(R.id.lastnameEditText);
		occupationEditText = (EditText) findViewById(R.id.occupationEditText);
		aboutmeEditText = (EditText) findViewById(R.id.aboutmeEditText);
		
		saveButton = (Button)findViewById(R.id.saveButton);
		saveButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				save();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.edit_account0, menu);
		return true;
	}
	
	private void showProgress(){
		loadingLayout.setVisibility(View.VISIBLE);
	}
	
	private void hideProgress(){
		loadingLayout.setVisibility(View.GONE);
	}
	
	private void save(){
		String firstname = firstnameEditText.getText().toString().trim();
		String lastname = lastnameEditText.getText().toString().trim();
		String occupation = occupationEditText.getText().toString().trim();
		String aboutme = aboutmeEditText.getText().toString().trim();
		if(firstname.equals("") || lastname.equals("")){
			toast(getResources().getString(R.string.type_firstname));
			return;
		}
		Map<String, String> params = new HashMap<String, String>();
        params.put("firstname", firstname);//TODO Put the id of the image selected here
        params.put("lastname", lastname);
        params.put("occupation", occupation);
        params.put("aboutme", aboutme);
		showProgress();
		CustomRequest jsObjRequest = new CustomRequest(
				Method.POST, 
				AppConstant.editaccountUrl, 
				params,
				new Response.Listener<JSONObject>() {
					 
                    @Override
                    public void onResponse(JSONObject response) {
                    	hideProgress();
                    	toast("Response : "+response.toString());
                    	Log.i("Test", response.toString());
                    	parseJsonSave(response);
                    }
                },
                new Response.ErrorListener() {
 
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    	hideProgress();
                    	toast("Error: " + error.getMessage());
                    	Log.e("Test", error.getMessage());
                    }
                }
        );

       AppController.getInstance().addToRequestQueue(jsObjRequest);
	}
	
	private void parseJsonSave(JSONObject response){
		try {
			if(response.has("result_code")){
				String resultCode = response.getString("result_code");
				if(resultCode.equals("success")){
					String success = response.getString("success");					
					toast(success);
					int id = response.getInt("id");
					String email = response.getString("email");
					String photo = response.getString("photo");
					//TODO update view and infos in userconnected
				}else if(response.has("error")){
	            	String error = response.getString("error");
	            	//TODO Show the error message in a textView and make it visible
	            	//loginErrorView.setText(error);
	            	//loginErrorView.setVisibility(View.VISIBLE);
	            	toast(error);
	            }else if(response.has("exception")){
	            	String error = response.getString("error");
	            	//TODO Show the error message in a textView and make it visible
	            	//loginErrorView.setText(error);
	            	//loginErrorView.setVisibility(View.VISIBLE);
	            	toast(error);
	            }
			}
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //hideProgress();
	}
	
	public void toast(String text){
		Toast.makeText(this, text,Toast.LENGTH_LONG).show();
	}
	

}
